package huxibianjie.com.lbscandroid.ui.new_activity.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.model.InviteRecordModel;
import huxibianjie.com.lbscandroid.util.Utils;


/**
 * 邀请记录
 * Created by CZF on 2018/8/2.
 */

public class InviteRecordAdapter extends RecyclerView.Adapter<InviteRecordAdapter.BaseViewHolder> {
    private List<InviteRecordModel> dataList = new ArrayList<>();
    private Activity activity;

    public InviteRecordAdapter(Activity activity) {
        this.activity = activity;
    }

    public void clearAll() {
        dataList.clear();
        notifyDataSetChanged();
    }

    public void replaceAll(@Nullable List<InviteRecordModel> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void addAll(@Nullable List<InviteRecordModel> list) {
        if (list != null && list.size() > 0) {
            int start = this.dataList.size();
            this.dataList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_invite_record, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.setData(dataList.get(position));
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }


    class BaseViewHolder extends RecyclerView.ViewHolder {
        private TextView tvPhone, tvGrade, tvLogin, tvProfit;

        BaseViewHolder(View itemView) {
            super(itemView);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            tvGrade = itemView.findViewById(R.id.tv_grade);
            tvLogin = itemView.findViewById(R.id.tv_login);
            tvProfit = itemView.findViewById(R.id.tv_profit);
        }

        private InviteRecordModel model;

        public void setData(Object data) {
            model = (InviteRecordModel) data;
            tvPhone.setText(Utils.starPhone(model.getPhone()));
            tvGrade.setText(String.valueOf(model.getLevel()));
            tvLogin.setText(String.valueOf("+" + model.getLoginOrRegiestReward()));
            tvProfit.setText(String.valueOf("+" + model.getDividend()));
        }

    }
}
